using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using TTDotNet.Util;
using TTDotNet.Engine.GUI.Buttons;
using TTDotNet.Engine.GUI.Windows.Road;
using TTDotNet.Engine.GUI;
using Microsoft.DirectX.Direct3D;
using TTDotNet.Engine.Create.Tools;

namespace TTDotNet.Engine.GUI.Windows
{
    public class RoadConcWindow : Window
    {
        private float bSize = 48;
        private float cSize = 24;
        private float border = 2;

        private BuildBusStationWindow bsWindow;
        private BuildRoadDepotWindow rdWindow;

        private int radioGroup;
        private int bsButtonNum;
        private int rdButtonNum;

        public RoadConcWindow(PointF screenLocation)
        {
            float width = (10 * bSize) + (9 * border);
            float height = cSize + bSize + (3 * border);

            SetScreenRectangle(new RectangleF(screenLocation, new SizeF(width, height)));

            int controlNum = 0;

            float x = screenRectangle.X;
            float y = screenRectangle.Y;
                          
            //Road Construction Panel.
            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            //Close button. 
            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - (cSize + (3 * border)), cSize)),
                                            "Road Construction", 14, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            RadioGroup group = new RadioGroup();
            radioGroup = controlNum;
            windowControls[controlNum++] = group;

            int rbNum = 0;

            //Build road.
            RadioButton buildRoad = new RadioButton(1, new RectangleF(new PointF(border, cSize + (2 * border)), new SizeF(bSize, bSize)), 
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/Road.jpg"), 
                                            Game.textureCacher.LoadTexture("artwork/128/Road_down.jpg"));
            buildRoad.OnControl += new Control.ControlDelegate(BuildRoad);
            
            group.Add(rbNum++, buildRoad);

            //Dynamite
            RadioButton dynamite = new RadioButton(1, new RectangleF(new PointF((2 * border) + (bSize + 1), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/dynamite.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/dynamite_down.jpg"));
            group.Add(rbNum++, dynamite);

            //Build road depot.
            RadioButton depot = new RadioButton(1, new RectangleF(new PointF((3 * border) + (bSize * 2), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/build_depot.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/build_depot_down.jpg"));
            depot.OnControl += new Control.ControlDelegate(CreateRoadDepotTool);
            rdButtonNum = rbNum;
            group.Add(rbNum++, depot);

            ////Build bus station.
            RadioButton busStation = new RadioButton(1, new RectangleF(new PointF((4 * border) + (bSize * 3), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/build_bus_station.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/build_bus_station_down.jpg"));
            busStation.OnControl += new Control.ControlDelegate(CreateBusStationWindow);
            bsButtonNum = rbNum;
            group.Add(rbNum++, busStation);

            ////Build lorry bay.
            RadioButton lorryStation = new RadioButton(1, new RectangleF(new PointF((5 * border) + (bSize * 4), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/build_lorry_bay.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/build_lorry_bay_down.jpg"));
            group.Add(rbNum++, lorryStation);

            ////Build bridge.
            RadioButton roadBridge = new RadioButton(1, new RectangleF(new PointF((6 * border) + (bSize * 5), cSize + (2 * border)), new SizeF(bSize * 2, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/256/bridge.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/256/bridge_down.jpg"));
            group.Add(rbNum++, roadBridge);

            ////Tunnel
            RadioButton roadTunnel = new RadioButton(1, new RectangleF(new PointF((7 * border) + (bSize * 7), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/tunnel.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/tunnel_down.jpg"));
            roadTunnel.OnControl += new Control.ControlDelegate(CreateRoadTunnelTool);
            group.Add(rbNum++, roadTunnel);

            ////Bulldozer
            windowControls[controlNum++] = new RadioButton(1, new RectangleF(new PointF((8 * border) + (bSize * 8), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/bulldozer.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/bulldozer_down.jpg"));

            ////Landscaping
            windowControls[controlNum++] = new GameButton(new RectangleF(new PointF((9 * border) + (bSize * 9), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", Game.textureCacher.LoadTexture("artwork/128/meander.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/meander_down.jpg"));
            windowControls[controlNum - 1].OnControl += new Control.ControlDelegate(CreateLandscapingWindow);

            
        }

        private void CreateLandscapingWindow(object data, object tag)
        {
            LandscapingWindow lWindow = new LandscapingWindow(new PointF(screenRectangle.X + 100, screenRectangle.Y + 100));
            Game.newGUI.OpenNewWindow(lWindow);
        }

        private void CreateBusStationWindow(object data, object tag)
        {
            ControlStateChange state = (ControlStateChange)data;

            if (state == ControlStateChange.Selected)
            {
                if (Game.newGUI.IsOpen(bsWindow) == false || bsWindow == null)
                {
                    bsWindow = new BuildBusStationWindow(new PointF(screenRectangle.X + 100, screenRectangle.Y + 100));
                    Game.newGUI.OpenNewWindow(bsWindow);
                }
            }
            else
            {
                if (Game.newGUI.IsOpen(bsWindow)) bsWindow.Close();
            }
        }

        private void CreateRoadDepotTool(object data, object tag)
        {
            ControlStateChange state = (ControlStateChange)data;

            if (state == ControlStateChange.Selected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.RoadDepot, true);
            }
            else
            {
                Game.toolbox.ChangeToolStatus(ToolType.RoadDepot, false);
            }
        }

        private void CreateRoadTunnelTool(object data, object tag)
        {
            ControlStateChange state = (ControlStateChange)data;

            if (state == ControlStateChange.Selected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.RoadTunnel, true);
            }
            else
            {
                Game.toolbox.ChangeToolStatus(ToolType.RoadTunnel, false);
            }
        }

        private void BuildRoad(object data, object tag)
        {
            ControlStateChange state = (ControlStateChange)data;

            if (state == ControlStateChange.Selected)
            {
                Game.toolbox.ChangeToolStatus(TTDotNet.Engine.Create.Tools.ToolType.Road, true);
            }
            else
            {
                Game.toolbox.ChangeToolStatus(TTDotNet.Engine.Create.Tools.ToolType.Road, false);
            }
        }

        public override void Update()
        {
            if (!Game.newGUI.IsOpen(bsWindow))
            {
                (windowControls[radioGroup] as RadioGroup).Deselect(bsButtonNum);
            }
        }

        public override void Close()
        {
            Game.toolbox.ChangeToolStatus(TTDotNet.Engine.Create.Tools.ToolType.Road, false);
            
            if (Game.newGUI.IsOpen(bsWindow)) bsWindow.Close();
            if (Game.newGUI.IsOpen(rdWindow)) rdWindow.Close();
            
            base.Close();
        }

    }
   
}
